package cn.android_mobile.core.ui;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.android_mobile.core.BasicActivity;

import com.ygsoft.smartfast.android.MResource;
/**
 * 
 * 全局标题.<br>
 * @author Administrator <br>
 * @version 1.0.0 2015年3月26日<br>
 * @see 
 * @since JDK 1.5.0
 */
public class NavigationBar implements View.OnClickListener {
	//标题
	public TextView titleText;
	
	//左边布局
	public ImageView leftBtn;
	
	//右边图标
	public ImageView rightIcon;
	//右边文字内容
	public TextView rightBtn;
	
	public RelativeLayout layout;
	
	private boolean isHidden = false;
	protected ViewStub vs;
	protected View view;
	protected INavigationBar callback;
	private BasicActivity act;

	public NavigationBar(BasicActivity act, ViewStub vs) {
		this.act = act;
		this.vs = vs;
		View view = initWithlayoutResID(MResource.getIdByName(act, "layout", "top_title_layout"));
		layout = (RelativeLayout) view.findViewById(MResource.getIdByName(act, "id", "title_relative"));
		
		leftBtn = (ImageView) view.findViewById(MResource.getIdByName(act, "id", "left_icon"));

		rightIcon = (ImageView) view.findViewById(MResource.getIdByName(act, "id", "right_icon"));
		rightBtn = (TextView) view.findViewById(MResource.getIdByName(act, "id", "right_text"));
		
		titleText = (TextView) view.findViewById(MResource.getIdByName(act, "id", "title_textView"));
		
		
		leftBtn.setOnClickListener(this);
		rightBtn.setOnClickListener(this);
	}

	public View initWithlayoutResID(int resId) {
		vs.setLayoutResource(resId);
		view = vs.inflate();
		return view;
	}
	
	/**
	 * 获取标题栏的高度，返回 px值
	 * @return
	 */
	public int getNavigationBarHeight(){
		
		return view.getHeight();
	}

	public void hidden() {
		isHidden = true;
		vs.setVisibility(View.GONE);
	}

	public void display() {
		isHidden = false;
		vs.setVisibility(View.VISIBLE);
	}

	public boolean isHidden() {
		return isHidden;
	}

	public void setBackground(int resid) {
		layout.setBackgroundResource(resid);
	}

	public void setBackground(String color) {
		layout.setBackgroundColor(Color.parseColor(color));
	}

	public void addListener(INavigationBar listener) {
		callback = listener;
	}

	@Override
	public void onClick(View v) {
		if (callback == null)
			return;
		callback.navigationBarClickHandler(v.getId());
		callback.navigationBarClickHandler(v);
	}

	public void hiddenButtons() {
		leftBtn.setVisibility(View.GONE);
		rightBtn.setVisibility(View.GONE);
	}

	public void displayButtons() {
		leftBtn.setVisibility(View.VISIBLE);
		rightBtn.setVisibility(View.VISIBLE);
	}

	public void hiddenLeftButton() {
		leftBtn.setVisibility(View.GONE);
	}

	public void hiddenRightButton() {
		rightBtn.setVisibility(View.GONE);
	}
	
	public void displayRightIcon() {
		rightIcon.setVisibility(View.VISIBLE);
	}
	
	public void hiddenRightIcon() {
		rightIcon.setVisibility(View.GONE);
	}

	public void displayLeftButton() {
		leftBtn.setVisibility(View.VISIBLE);
	}

	public void displayRightButton() {
		rightBtn.setVisibility(View.VISIBLE);
	}

	public void displayTitle() {
		titleText.setVisibility(View.VISIBLE);
	}

	public void hiddenTitle() {
		titleText.setVisibility(View.GONE);
	}

	public void setTitle(String s) {
		titleText.setText(s);
	}

	public void setTitle(int strId) {
		titleText.setText(strId);
	}

	public void setTextLeftButton(String s) {
		displayLeftButton();
	}

	public void setTextRightButton(String s) {
		rightBtn.setText(s);
	}
	
	public void setRightButtonColor(int color) {
		rightBtn.setTextColor(color);
	}
	
	public void setTextRightButtonSize(int sp) {
		rightBtn.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
	}

	/**
	 * 显示左边图标，并且把右边按钮隐藏
	 * @param imageId 图片资源id
	 */
	public void setLeftIcon(int imageId){
		leftBtn.setImageResource(imageId);
	}
	/**
	 * 获取标题文字.
	 * @return 返回标题文字.
	 */
	public String getTitleText() {
		return titleText.getText().toString();
	}
	/*public void setLeftIcon(Drawable left, Drawable top, Drawable right,
			Drawable bottom) {
		if (left != null)
			left.setBounds(0, 0, left.getMinimumWidth(),
					left.getMinimumHeight());
		if (right != null)
			right.setBounds(0, 0, right.getMinimumWidth(),
					right.getMinimumHeight());
		if (top != null)
			top.setBounds(0, 0, top.getMinimumWidth(), top.getMinimumHeight());
		if (bottom != null)
			bottom.setBounds(0, 0, bottom.getMinimumWidth(),
					bottom.getMinimumHeight());
		leftIcon.setCompoundDrawables(left, top, right, bottom);
	}*/

	/*public void setLeftIcon(Drawable left, Drawable top, Drawable right,
			Drawable bottom, int w, int h) {
		if (left != null){
			left.setBounds(0, 0, act.dip2px(w), act.dip2px(h));
			leftBtn.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
		}
		if (right != null){
			right.setBounds(0, 0, act.dip2px(w), act.dip2px(h));
			leftBtn.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
		}
		if (top != null)
			top.setBounds(0, 0, act.dip2px(w), act.dip2px(h));
		if (bottom != null)
			bottom.setBounds(0, 0, act.dip2px(w), act.dip2px(h));
		leftBtn.setCompoundDrawables(left, top, right, bottom);
	}*/

	/*public void setRightIcon(Drawable left, Drawable top, Drawable right,
			Drawable bottom) {
		if (left != null)
			left.setBounds(0, 0, left.getMinimumWidth(),
					left.getMinimumHeight());
		if (right != null)
			right.setBounds(0, 0, right.getMinimumWidth(),
					right.getMinimumHeight());
		if (top != null)
			top.setBounds(0, 0, top.getMinimumWidth(), top.getMinimumHeight());
		if (bottom != null)
			bottom.setBounds(0, 0, bottom.getMinimumWidth(),
					bottom.getMinimumHeight());
		rightBtn.setCompoundDrawables(left, top, right, bottom);
	}*/

	/*public void setRightIcon(Drawable left, Drawable top, Drawable right,
			Drawable bottom, int w, int h) {
		if (left != null) {
			left.setBounds(0, 0, act.dip2px(w), act.dip2px(h));
			rightBtn.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
		}
		if (right != null){
			right.setBounds(0, 0, act.dip2px(w), act.dip2px(h));
			rightBtn.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
		}
		if (top != null){
			top.setBounds(0, 0, act.dip2px(w), act.dip2px(h));
		}
		if (bottom != null){
			bottom.setBounds(0, 0, act.dip2px(w), act.dip2px(h));
		}
		rightBtn.setCompoundDrawables(left, top, right, bottom);
	}	*/
	
	/**
	 * 显示右边图标，并且把右边按钮隐藏
	 * @param imageId 图片资源id
	 */
	public void setRightIcon(int imageId) {
		rightIcon.setImageResource(imageId);
	}
	
	
}
